window.wns = window.wns || {};

wns.hasLocate = function () {
    return !!$.cookie("locale");
};
wns.setLocate = function (value) {
    if (!value) return;
    $.cookie("locale", value, { path: "/", expires: 30 });
};
wns.getCookie = function (name) {
    const objects = $.cookie();
    if (name in objects) return objects[name];
    return objects;
};
wns.initLocale = function () {
    const value = wns.getCookie("locale");
    const spanEl = $("#language span");
    if (spanEl.length) {
        const locale = value == "zh-CN" ? "zh-CN" : "en";
        const text = value == "zh-CN" ? "中文" : "English";
        spanEl.text(text).attr("data-locale", locale);
    }
};
wns.changeLocale = function () {
    const langEl = $("#language");
    if (!langEl.length) return;
    // 发送请求或执行其他逻辑来切换语言
    langEl.on("click", function () {
        const value = langEl.find("span").attr("data-locale");
        const locale = value == "zh-CN" ? "en" : "zh-CN";
        wns.setLocate(locale);
        location.reload(); // 刷新页面以更新翻译文本
    });
};

wns.swiperInit = function (e, options = {}) {
    let defaults = {
        autoplay: false,
        speed: 800,
        loop: true,
    };
    if (options) {
        defaults = Object.assign(defaults, options);
    }
    new Swiper(e, defaults);
};

wns.aboutInit = function (e) {
    const el = $(e);
    if (!el.length) return;
    el.find(".item .con").hover(function () {
        el.find(".item").removeClass("on");
        $(this).parents(".item").addClass("on");
    });
};
wns.setPoint = function () {
    const hash = location.hash;
    if (!hash) return;
    $(`[href=${hash}]`).click();
};
wns.changeTab = function (e) {
    $(e).click(function () {
        const name = $(this).attr("href").substring(1);
        const contEl = $(`.${name}`);
        if (contEl.length) {
            contEl.show().siblings().hide();
        }
        if (!$(this).hasClass("on")) {
            $(this).addClass("on").siblings().removeClass("on");
        }
    });
};
wns.clickType = function (success) {
    const em = $(".chose_title .item em");
    $(em).click(function (e) {
        const val = e.dataset.id;
        success(val);
    });
};
wns.http = function (data, success) {
    const url = location.href;
    request.post(url, data, success);
};
wns.render = function (data, callback) {
    const result = data.map((item) => callback(item));
    result.push('<div class="c"></div>'); // clear float
    return result.join("");
};

$(function () {
    // !wns.hasLocate() && wns.setLocate("zh-CN")
    // wns.initLocale()
    // wns.changeLocale()

    // 高亮当前栏目
    mb.currentMenu("#head .head_nav > .wp > a", function () {
        const search = new URLSearchParams(window.location.search);

        let root = $(".web_list_page");
        if (!root) return;

        if (search.has("category_id")) {
            let categoryId = search.get("category_id");
            let target = root.find(
                `.chose_title .item > em[data-id=${categoryId}]`
            );
            target.addClass("on").siblings().removeClass("on");
        }

        if (search.has("tag")) {
            let tag = search.get("tag");
            let target = root.find(`.chose_title .item > em[data-tag=${tag}]`);
            target.addClass("on").siblings().removeClass("on");
        }
    });

    // home - module - tab
    mb.home.tab("span[data-tab]");

    /**
     * @url /content/{type}?category_id={category_id}&tag={tag}
     * @param category_id
     * @param tag
     */
    $(".web_list_page .chose_title .item > em").click(function ({ target }) {
        const data = target.dataset;

        const params = new URLSearchParams(location.search);
        if (data.hasOwnProperty("id")) {
            if (+data.id === 0) params.delete("category_id");
            else params.set("category_id", data.id);
        }
        if (data.hasOwnProperty("tag")) {
            if (data.tag == "全部") params.delete("tag");
            else params.set("tag", data.tag);
        }
        params.delete("page");
        const url = location.pathname + "?" + params.toString();
        window.location.replace(url);
    });

    $("#web_head_mm .open_nav").click(function (event) {
        $(this).toggleClass("on");
        $(this).next().toggleClass("on");
        $("#web_head_mm .bg").unbind("click");
        $("#web_head_mm .bg").click(function () {
            const nav = $("#web_head_mm .open_nav");
            nav.toggleClass("on");
            nav.next().toggleClass("on");
            $(this).unbind("click");
        });
    });

    $("#scrolltop").click(function () {
        $("html,body").animate({ scrollTop: 0 }, 500);
    });
});
